package com.andy.customerService.service.impl;


import com.andy.customerService.entity.Org;
import com.andy.customerService.enums.ExceptionEnum;
import com.andy.customerService.exception.BusinessException;
import com.andy.customerService.mapper.OrgMapper;
import com.andy.customerService.pojo.request.OrgRequestParam;
import com.andy.customerService.service.OrgService;
import com.andy.customerService.vo.JsonBaseResult;
import com.andy.customerService.vo.JsonPageResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 组织机构表 服务实现类
 * </p>
 *
 * @author Andy
 * @since 2021-12-27
 */
@Service
@Slf4j
public class OrgServiceImpl extends ServiceImpl<OrgMapper, Org> implements OrgService {

    @Autowired
    private OrgMapper orgMapper;

    /**
     * 添加组织机构
     *
     * @param org
     */
    @Transactional
    @Override
    public void addOrg(Org org) {
        log.info("系统管理-添加组织机构 入参：{}", org);
        orgMapper.insert(org);
    }

    /**
     * 根据id修改组织机构
     *
     * @param org
     */
    @Transactional
    @Override
    public void editOrgById(Org org) {
        log.info("系统管理-根据id修改组织机构 入参：{}", org);
        //不允许修改机构代码
        org.setCode(null);
        if (null == org.getId()) {
            throw new BusinessException(ExceptionEnum.ORG_ID_CAN_NOT_BE_NULL);
        }
        orgMapper.updateById(org);
    }

    /**
     * 根据id删除组织机构
     *
     * @param id
     */
    @Transactional
    @Override
    public void deleteOrgById(Long id) {
        log.info("系统管理-根据id删除组织机构 入参：{}", id);
        orgMapper.deleteById(id);
    }

    /**
     * 根据id查询组织机构
     *
     * @param id
     */
    @Override
    public JsonBaseResult<Object> queryOrgById(Long id) {
        log.info("根据id查询组织机构 入参：{}", id);
        JsonBaseResult<Object> result = new JsonBaseResult<>();
        Org org = orgMapper.selectById(id);
        result.setResult(org);
        return result;
    }

    /**
     * 根据条件分页查询组织机构
     *
     * @param requestParam
     */
    @Override
    public JsonPageResult<Object> queryOrgByParam(OrgRequestParam requestParam) {
        log.info("根据条件查询组织机构 入参：{}",requestParam);
        // 配置分页
        Page<Org> page = PageHelper.startPage(requestParam.getPageNo(),requestParam.getPageSize());
        //列表查询
        List<Org> orgList = orgMapper.queryOrgByParam(requestParam);

        //封装结果
        JsonPageResult<Object> pageResult = new JsonPageResult<>();
        pageResult.setResult(orgList);
        int totalNum = (int) page.getTotal();
        pageResult.setTotalNum(totalNum);
        pageResult.setPageNo(requestParam.getPageNo());
        pageResult.setPageSize(requestParam.getPageSize());
        return pageResult;
    }
}
